//
//  CloudMineViewController.m
//  CloudMine
//
//  Created by Hilary Muludiang on 2/26/12.
//  Copyright 2012 Versatile Systems, Inc. All rights reserved.
//

#import "CloudMineViewController.h"

@implementation CloudMineViewController

@synthesize rolesArray;
@synthesize getRolesButton;



-(IBAction)whatRoles:(id)sender{
	[self cloudMine:@"Get" contentType:@"json"];
	
}

-(void)cloudMine:(NSString *)action contentType:(NSString *)inputType{
	// logic to handle requests to CloudMine
	NSString *const APIKey = @"9c4d0d735525435489e26b316d1cd03f";
	NSString *const AppID = @"f77a584fbc9a4eae99abdab0672197b0";
	NSString *const URLBase = [NSString stringWithFormat:@"https://api.cloudmine.me/v1/app/%@/text?count=true", AppID];
	
	// NSString *URLString = [NSString stringWithFormat:@"%@text -H X-CloudMine-ApiKey:%@ -X %@", URLBase, APIKey, action];
	// [NSString stringByAddingPercentEscapesUsingEncoding:NSASCIIS tringEncoding]]

	// Create NSURL string from formatted string
	// [string  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	// NSURL *URLRequest = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
	
	// NSLog(@"URLRequest: %@", URLRequest);
	
	// Setup and start async download
	NSMutableURLRequest *testURL = [[NSMutableURLRequest alloc] init ];
	[testURL setURL:[NSURL URLWithString:URLBase]]; 
	[testURL setHTTPMethod:action];
	[testURL setValue:APIKey forHTTPHeaderField:@"X-CloudMine-ApiKey"];
	[testURL setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

	
	NSLog(@"testURL: %@", [testURL URL]);
	
	// setURL:[NSURL URLWithString:kWebURL@"/req/request.php"]];
    // [request setHTTPMethod:@"POST"];
    // [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    // [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
	
	
	// NSURLRequest *request = [[NSURLRequest alloc] initWithURL: testURL];
	NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:testURL delegate:self];
	[connection release];
	// [request release];
	

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
	// Store incoming data into a string
	NSString *jsonResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	NSLog(@"jsonString: %@", jsonResult);
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
